// dllmain.h : Declaration of module class.

class CComPCSelfy : public ATL::CComObject< CPCSelfy >
{
public:
	STDMETHOD(InterfaceSupportsErrorInfo) (REFIID riid) throw()
	{
		static const IID* const arr[] = 
		{
			&IID_IDispatch,
			&IID_IUnknown,	//	&IID_IPCSelfy
		};

		for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
		{
			if (::InlineIsEqualGUID(*arr[i],riid))
				return S_OK;
		}
		return S_FALSE;
	}

	STDMETHOD(QueryInterface) (REFIID aniid, void** ppv) throw()
	{
		if (ppv == NULL) return E_POINTER;
//		HRESULT hRes = _InternalQueryInterface(aniid, ppv);
//		return hRes;	E_NOINTERFACE
		* ppv = static_cast<IDispatch *>(this);
		HRESULT hRes = (* ppv) ? S_OK : E_POINTER;
		if (* ppv)
		if (::InlineIsEqualGUID(IID_ISupportErrorInfo, aniid))
			* ppv = static_cast<ISupportErrorInfo *>(this);
		else
			hRes = InterfaceSupportsErrorInfo(aniid);

		if (hRes ^ S_OK) return E_NOINTERFACE;
			InternalAddRef();
			//	AddRef();	//	IDispatchEx ?
		return hRes;
	}
};

HRESULT WINAPI NotAtlCreInstance(void* pv, REFIID riid, LPVOID* ppv)
{
	if (ppv == NULL) return E_POINTER;
		*ppv = NULL;

	CComPCSelfy * p = new CComPCSelfy;
	if (!p) return E_OUTOFMEMORY;

	p->SetVoid(pv);
	p->InternalFinalConstructAddRef();
	HRESULT hRes = p->_AtlInitialConstruct();
	if (SUCCEEDED(hRes)) hRes = p->FinalConstruct();
	if (SUCCEEDED(hRes)) hRes = p->_AtlFinalConstruct();
		p->InternalFinalConstructRelease();
	if (hRes == S_OK) hRes = p->QueryInterface(riid, ppv);
	if (hRes ^ S_OK) delete p;

	return hRes;
}
